#include "credits.h"

extern u8 font_ttf[];
extern u32 font_ttf_size;

extern u8 fan_art_1_png[];
extern u8 fan_art_2_png[];
extern u8 fan_art_3_png[];

#include <wiiuse/wpad.h>

#include <ogcsys.h>
#include <gccore.h>

Credits::Credits()
{

	_textRender = new TextRender();
	_textImage  = new DrawableImage();
	_textImage2  = new DrawableImage();

	_textRender->SetFont(font_ttf, font_ttf_size);

	_textImage->CreateImage(640, 128);
	_textImage->SetFont(_textRender);
	_textImage->SetColor((GXColor){0,0,0,255});
	_textImage->SetSize(30);		

	_textImage2->CreateImage(640, 128);
	_textImage2->SetFont(_textRender);
	_textImage2->SetColor((GXColor){0,0,0,255});
	_textImage2->SetSize(30);

	Image* fanImage1 = new Image();
	if(fanImage1->LoadImage(fan_art_1_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	_fanArtSprites.push_back(new Sprite());
	_fanArtSprites[0]->SetImage(fanImage1);
	_fanArtSprites[0]->SetPosition(30, 30);

	Image* fanImage2 = new Image();
	if(fanImage2->LoadImage(fan_art_2_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	_fanArtSprites.push_back(new Sprite());
	_fanArtSprites[1]->SetImage(fanImage2);
	_fanArtSprites[1]->SetPosition(200, 100);
	
	Image* fanImage3 = new Image();
	if(fanImage3->LoadImage(fan_art_3_png, IMG_LOAD_TYPE_BUFFER) != IMG_LOAD_ERROR_NONE)exit(0);
	
	_fanArtSprites.push_back(new Sprite());
	_fanArtSprites[2]->SetImage(fanImage3);
	_fanArtSprites[2]->SetPosition(30, 100);
	
	_credits.push_back("  Thanks for Playing! ");
	_credits.push_back("                      ");
	_credits.push_back("     Developers:      ");
	_credits.push_back("      Beardface       ");
	_credits.push_back("                      ");
	_credits.push_back("     Level Design:    ");
	_credits.push_back("      Beardface       ");
	_credits.push_back("         Qual         ");
	_credits.push_back("                      ");
	_credits.push_back("        Story:        ");
	_credits.push_back("      Beardface       ");
	_credits.push_back("                      ");
	_credits.push_back("       Graphics:      ");
	_credits.push_back("      Beardface       ");
	_credits.push_back("      Deviantart      ");
	_credits.push_back("                      ");
	_credits.push_back("        Music:        ");
	_credits.push_back("      'Evilness'      ");
	_credits.push_back("        Sergey        ");
	_credits.push_back("                      ");
	_credits.push_back("    'Shallow Space'   ");
	_credits.push_back("      nubbinownz      ");
	_credits.push_back("                      ");
	_credits.push_back("  '8-bit Still Alive' ");
	_credits.push_back("        Kris G        ");
	_credits.push_back("                      ");
	_credits.push_back("    Special Thanks:   ");
	_credits.push_back("       devkitPro      ");
	_credits.push_back("        libogc        ");
	_credits.push_back("   wiiuse, wiisprite  ");
	_credits.push_back(" Valve for Inspiration");
	_credits.push_back("                      ");
	_credits.push_back("          and         ");
	_credits.push_back("                      ");
	_credits.push_back("       Tehskeen!      ");
	_credits.push_back("                      ");
	_credits.push_back("                      ");
	_credits.push_back(" Hold A and B at the  ");
	_credits.push_back("  Beardface Intro...  ");
}

Credits::~Credits()
{

}

bool Credits::RollCredits(GameWindow* gw)
{
	int MaxDrift;
	int Drift = -480;
	
        bool swap = true;

        MaxDrift =40*_credits.size() + 80;

	Sprite drawSprite;
	
	int trans = 1;
	int index = 0;
	
	bool fade_out = false;
	
	int start_index = 0;
	
	while(1)
	{
		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(WPAD_CHAN_0);
	  
	   if(trans <= 0)
	   {
			trans = 1;
			fade_out = false;
			index++;
			index = index % _fanArtSprites.size();
	   }
	   else if(trans >= 255)
	   {
			fade_out = true;
	   }
	   
		if(fade_out)
		{
			trans-=5;
			if(trans < 0) trans = 0;
		}
		else
		{
			trans+=5;
			if(trans > 255) trans = 255;
		}
	   
	   _fanArtSprites[index]->SetTransparency(trans);
	   _fanArtSprites[index]->Draw();
	   
       if(pressed & WPAD_BUTTON_A || pressed & WPAD_BUTTON_HOME)
       {
		   return false;
	   }
	  
			for(int i = start_index; i < (int)_credits.size() && i < start_index + 14; i++)
			{
				if(swap)
				{
					_textImage->RenderText(_credits[i].c_str());
					drawSprite.SetImage(_textImage->GetImage());
					swap = false;
				}
				else
				{
					_textImage2->RenderText(_credits[i].c_str());
					drawSprite.SetImage(_textImage2->GetImage());
					swap = true;
				}
				drawSprite.SetPosition(100,(i*40 + 40));
				drawSprite.Draw(0, (-1)*Drift);
				
			}
			
			Drift+=5;
			
			if(Drift >= (120+(start_index)*40))
			{
				if(start_index < ((int)_credits.size() - 14))
				{
					start_index++;
				}
			}
			
			if(Drift > MaxDrift)
				return true;
			
			gw->Flush();
		
	}

}
